//
//  XQAFNetworking.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/23.
//

#import "XQAFNetworking.h"

@implementation XQAFNetworking

+ (void)getWithUrl:(NSString *)url pars:(NSDictionary *)pars callback:(XQAFNetworkingBlock)callback {
    [XQAFNetworking getWithUrl:url pars:pars auth:nil callback:callback];
}

+ (void)getWithUrl:(NSString *)url pars:(NSDictionary *)pars auth:(NSString *)auth callback:(XQAFNetworkingBlock)callback {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    if(auth){
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@ %@", @"Bearer", auth] forHTTPHeaderField:@"Authorization"];
    }
    
    [manager GET:url parameters:pars success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        callback(true,responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        callback(false,nil,error);
    }];
}

+ (void)postWithUrl:(NSString *)url pars:(NSDictionary *)pars callback:(XQAFNetworkingBlock)callback {
    [XQAFNetworking postWithUrl:url pars:pars auth:nil callback:callback];
}

+ (void)postWithUrl:(NSString *)url pars:(NSDictionary *)pars auth:(NSString *)auth callback:(XQAFNetworkingBlock)callback {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    if(auth){
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@ %@", @"Bearer", auth] forHTTPHeaderField:@"Authorization"];
    }
    
    [manager POST:url parameters:pars success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        callback(true,responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        callback(false,nil,error);
    }];
}

+ (void)uploadImageUrl:(NSString *)url pars:(NSDictionary *)pars imageData:(NSData *)imageData callback:(XQAFNetworkingBlock)callback {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    
    [manager POST:url parameters:pars constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (NSString *key in pars.allKeys) {
            // 循环拿到所有参数进行拼接
            [formData appendPartWithFormData:[pars[key] dataUsingEncoding:NSUTF8StringEncoding] name:key];
        }
        NSString *fileName = [NSString stringWithFormat:@"%f.png", [[NSDate date] timeIntervalSince1970]];
        [formData appendPartWithFileData:imageData name:@"kuConsists" fileName:fileName mimeType:@"image/png"];
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        callback(true,responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        callback(false,nil,error);
    }];
    
//    [manager POST:url param
//            eters:nil headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//       
//    } progress:^(NSProgress * _Nonnull uploadProgress) {
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//       
//    }];
}


@end
